<?php //pack11class

class LeftMenu extends Module {

    private $current;
    private $sqlQuery;

    private $defined=array();
    private $generated=array();
    private $labelPlaced=false;

	public function setup() {
      //$this->traceEnabled=true;
       $this->sqlQuery=History::$sqlQuery;
       
        if(History::$current) {
            $this->current=PropertiesManager::getChild(History::$current);
            $this->draw($this->current);
        }
    }

    private function draw(Property $property) {

        if($property->isValue) return;

        $table=MySQL::TABLE_OBJECTS;
        $join=MySQL::TABLE_VALUES;
        $lang=LanguagesManager::sql();

        if(!$this->labelPlaced) {
            $label=new LeftMenuLabel;
            $label->setBody(ValuesManager::getValueByID(History::$current->value));
        }

        $_query=$this->generateSQLQuery($property);
        //trace($_query);

        while(!MySQLGet::process("Select count({$table}.`{$property->field}`) as `count`, {$table}.`{$property->field}` as `valueid`, {$join}.{$lang}  from {$table} left join {$join} on ({$join}.`id`={$table}.`{$property->field}`) {$_query} group by `value`;")) {
            $property=PropertiesManager::getChild($property);
            if(!$property) return;
            $_query=$this->generateSQLQuery($property);
        }
            //trace(MySQLGet::getQuery());

        $result=MySQLGet::$result;
        foreach($result as $row) {
            $item= new LeftMenuItem;
            //trace("ValueID: ".(is_null($row["valueid"])? "null":$row["valueid"]));
            if(is_null($row["valueid"])) {
                //trace("!!!NULL!!!");
                array_push($this->sqlQuery,"`{$property->field}` is null");
                $this->draw(PropertiesManager::getChild($property));
            } else {
                $_property=clone $property;
                $_property->value=$row["valueid"];
                $queryObject=clone History::$queryObject;
                $queryObject->setValue($_property);
                /*if($_setting=SettingsManager::getSetting("list")) {
                    if(!isset($_setting->value)) $_setting->value=1;
                    $queryObject->setValue($_setting,GetQueryObject::SETTING);
                }*/

                $getquery=GetQuery::generate($queryObject);
                $item->link="?state=".$getquery;
                //GetQuery::translate($getquery);
                //$item->setBody($row["value"]);
                $item->setBody(ValuesManager::getValueByID($row["valueid"]));
                $item->count=$row["count"];

                    if(!$this->labelPlaced) {
                        $this->addChild($label);
                        $this->labelPlaced=true;
                        $this->addStyle("padding-bottom", "20px");
                    }

                $this->addChild($item);
            }
        }

    }

    private function generateSQLQuery(Property $property) {
        $table=MySQL::TABLE_OBJECTS;
        return (count($this->sqlQuery)>0)? "where ".implode(" AND ",$this->sqlQuery)." AND `active`='1'":"where {$table}.`{$property->field}` AND `active`='1'";
    }

}
?>
